package com.luo.backend.dto;

import com.luo.backend.entity.Order;
import com.luo.backend.entity.OrderItem;
import com.fasterxml.jackson.annotation.JsonFormat;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 订单响应DTO
 */
public class OrderResponse {
    
    private Long id;
    private String orderNo;
    private Long userId;
    private String username;
    private Long merchantId;
    private String merchantName;
    private String deliveryAddress;
    private String deliveryPhone;
    private String deliveryName;
    private BigDecimal foodAmount;
    private BigDecimal deliveryFee;
    private BigDecimal totalAmount;
    private String status;
    private String statusText;
    
    @JsonFormat(pattern = "yyyy-MM-dd")
    private LocalDate scheduledDeliveryDate;
    
    private String scheduledDeliveryTime;
    private String notes;
    private String cancelReason;
    private String refundReason;
    
    @JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss")
    private LocalDateTime paidAt;
    
    @JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss")
    private LocalDateTime acceptedAt;
    
    @JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss")
    private LocalDateTime shippedAt;
    
    @JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss")
    private LocalDateTime actualDeliveryTime;
    
    @JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss")
    private LocalDateTime refundedAt;
    
    @JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss")
    private LocalDateTime cancelledAt;
    
    @JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss")
    private LocalDateTime createdAt;
    
    private List<OrderItemResponse> items;
    
    // 内部类 - 订单商品响应
    public static class OrderItemResponse {
        private Long id;
        private Long foodId;
        private String foodName;
        private String foodImage;
        private BigDecimal foodPrice;
        private Integer quantity;
        private String spiceLevel;
        private BigDecimal subtotal;
        
        public OrderItemResponse(OrderItem item) {
            this.id = item.getId();
            this.foodId = item.getFoodId();
            this.foodName = item.getFoodName();
            this.foodImage = item.getFoodImage();
            this.foodPrice = item.getFoodPrice();
            this.quantity = item.getQuantity();
            this.spiceLevel = item.getSpiceLevel();
            this.subtotal = item.getSubtotal();
        }
        
        // Getters and Setters
        public Long getId() { return id; }
        public void setId(Long id) { this.id = id; }
        
        public Long getFoodId() { return foodId; }
        public void setFoodId(Long foodId) { this.foodId = foodId; }
        
        public String getFoodName() { return foodName; }
        public void setFoodName(String foodName) { this.foodName = foodName; }
        
        public String getFoodImage() { return foodImage; }
        public void setFoodImage(String foodImage) { this.foodImage = foodImage; }
        
        public BigDecimal getFoodPrice() { return foodPrice; }
        public void setFoodPrice(BigDecimal foodPrice) { this.foodPrice = foodPrice; }
        
        public Integer getQuantity() { return quantity; }
        public void setQuantity(Integer quantity) { this.quantity = quantity; }
        
        public String getSpiceLevel() { return spiceLevel; }
        public void setSpiceLevel(String spiceLevel) { this.spiceLevel = spiceLevel; }
        
        public BigDecimal getSubtotal() { return subtotal; }
        public void setSubtotal(BigDecimal subtotal) { this.subtotal = subtotal; }
    }
    
    // 构造函数
    public OrderResponse() {}
    
    public OrderResponse(Order order) {
        this.id = order.getId();
        this.orderNo = order.getOrderNo();
        this.userId = order.getUserId();
        this.merchantId = order.getMerchantId();
        this.merchantName = order.getMerchantName();
        this.deliveryAddress = order.getDeliveryAddress();
        this.deliveryPhone = order.getDeliveryPhone();
        this.deliveryName = order.getDeliveryName();
        this.foodAmount = order.getFoodAmount();
        this.deliveryFee = order.getDeliveryFee();
        this.totalAmount = order.getTotalAmount();
        this.status = order.getStatus();
        this.statusText = getStatusText(order.getStatus());
        this.scheduledDeliveryDate = order.getScheduledDeliveryDate();
        this.scheduledDeliveryTime = order.getScheduledDeliveryTime();
        this.notes = order.getNotes();
        this.cancelReason = order.getCancelReason();
        this.refundReason = order.getRefundReason();
        this.paidAt = order.getPaidAt();
        this.acceptedAt = order.getAcceptedAt();
        this.shippedAt = order.getShippedAt();
        this.actualDeliveryTime = order.getActualDeliveryTime();
        this.refundedAt = order.getRefundedAt();
        this.cancelledAt = order.getCancelledAt();
        this.createdAt = order.getCreatedAt();
    }
    
    private String getStatusText(String status) {
        switch (status) {
            case "pending": return "待支付";
            case "paid": return "已支付";
            case "preparing": return "准备中";
            case "shipping": return "配送中";
            case "delivered": return "已送达";
            case "cancelled": return "已取消";
            case "refunded": return "已退款";
            default: return "未知状态";
        }
    }
    
    // Getters and Setters
    public Long getId() { return id; }
    public void setId(Long id) { this.id = id; }
    
    public String getOrderNo() { return orderNo; }
    public void setOrderNo(String orderNo) { this.orderNo = orderNo; }
    
    public Long getUserId() { return userId; }
    public void setUserId(Long userId) { this.userId = userId; }
    
    public String getUsername() { return username; }
    public void setUsername(String username) { this.username = username; }
    
    public Long getMerchantId() { return merchantId; }
    public void setMerchantId(Long merchantId) { this.merchantId = merchantId; }
    
    public String getMerchantName() { return merchantName; }
    public void setMerchantName(String merchantName) { this.merchantName = merchantName; }
    
    public String getDeliveryAddress() { return deliveryAddress; }
    public void setDeliveryAddress(String deliveryAddress) { this.deliveryAddress = deliveryAddress; }
    
    public String getDeliveryPhone() { return deliveryPhone; }
    public void setDeliveryPhone(String deliveryPhone) { this.deliveryPhone = deliveryPhone; }
    
    public String getDeliveryName() { return deliveryName; }
    public void setDeliveryName(String deliveryName) { this.deliveryName = deliveryName; }
    
    public BigDecimal getFoodAmount() { return foodAmount; }
    public void setFoodAmount(BigDecimal foodAmount) { this.foodAmount = foodAmount; }
    
    public BigDecimal getDeliveryFee() { return deliveryFee; }
    public void setDeliveryFee(BigDecimal deliveryFee) { this.deliveryFee = deliveryFee; }
    
    public BigDecimal getTotalAmount() { return totalAmount; }
    public void setTotalAmount(BigDecimal totalAmount) { this.totalAmount = totalAmount; }
    
    
    public String getStatus() { return status; }
    public void setStatus(String status) { this.status = status; }
    
    public String getStatusText() { return statusText; }
    public void setStatusText(String statusText) { this.statusText = statusText; }
    
    public LocalDate getScheduledDeliveryDate() { return scheduledDeliveryDate; }
    public void setScheduledDeliveryDate(LocalDate scheduledDeliveryDate) { this.scheduledDeliveryDate = scheduledDeliveryDate; }
    
    public String getScheduledDeliveryTime() { return scheduledDeliveryTime; }
    public void setScheduledDeliveryTime(String scheduledDeliveryTime) { this.scheduledDeliveryTime = scheduledDeliveryTime; }
    
    public String getNotes() { return notes; }
    public void setNotes(String notes) { this.notes = notes; }
    
    public String getCancelReason() { return cancelReason; }
    public void setCancelReason(String cancelReason) { this.cancelReason = cancelReason; }
    
    public String getRefundReason() { return refundReason; }
    public void setRefundReason(String refundReason) { this.refundReason = refundReason; }
    
    public LocalDateTime getPaidAt() { return paidAt; }
    public void setPaidAt(LocalDateTime paidAt) { this.paidAt = paidAt; }
    
    public LocalDateTime getAcceptedAt() { return acceptedAt; }
    public void setAcceptedAt(LocalDateTime acceptedAt) { this.acceptedAt = acceptedAt; }
    
    public LocalDateTime getShippedAt() { return shippedAt; }
    public void setShippedAt(LocalDateTime shippedAt) { this.shippedAt = shippedAt; }
    
    public LocalDateTime getActualDeliveryTime() { return actualDeliveryTime; }
    public void setActualDeliveryTime(LocalDateTime actualDeliveryTime) { this.actualDeliveryTime = actualDeliveryTime; }
    
    public LocalDateTime getRefundedAt() { return refundedAt; }
    public void setRefundedAt(LocalDateTime refundedAt) { this.refundedAt = refundedAt; }
    
    public LocalDateTime getCancelledAt() { return cancelledAt; }
    public void setCancelledAt(LocalDateTime cancelledAt) { this.cancelledAt = cancelledAt; }
    
    public LocalDateTime getCreatedAt() { return createdAt; }
    public void setCreatedAt(LocalDateTime createdAt) { this.createdAt = createdAt; }
    
    public List<OrderItemResponse> getItems() { return items; }
    public void setItems(List<OrderItemResponse> items) { this.items = items; }
}
